package com.example.origin.advice.controller;

import io.swagger.models.auth.In;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Positive;

/**
 * @author yutao.zhang
 * @create 2023-07-25 16:27
 **/
@Validated
@RestController
public class DemoController {

    @GetMapping(value = "demo")
    public String demo(@RequestParam String msg) {
        return msg;
    }

    @GetMapping("error")
    public String error(@RequestParam(required = false, defaultValue = "0") Integer flag) {
        if (flag == 1) {
            int i = 1 / 0;
        }
        return "";
    }

    @GetMapping(value = "user")
    public User user(@RequestParam(required = false) @NotBlank(message = "name required") String name,
                     @RequestParam(required = false) @NotNull(message = "age required") Integer age) {
        return new User(name,age);
    }

    @PostMapping(value = "addUser")
    public User addUser(@RequestBody @Validated User user) {
        return user;
    }

    @Data
    @AllArgsConstructor
    private static class User{
        @NotBlank(message = "name required")
        private String name;
        @NotNull(message = "age required")
        private Integer age;
    }
}
